package HeSuanServer.service.Impl;

import HeSuanServer.POJO.R.Result;
import HeSuanServer.POJO.R.StatusCode;
import HeSuanServer.POJO.User;
import HeSuanServer.POJO.UserMessage;
import HeSuanServer.mapper.UserCliMapper;
import HeSuanServer.service.UserCliService;
import HeSuanServer.uitl.NumberCodeUtil;
import cn.hutool.core.date.DateTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @描述:
 * @author: LH
 * @date: 2023/3/14
 */
@Service
@Slf4j
public class UserCliServiceImpl implements UserCliService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private UserCliMapper userCliMapper;
    @Override
    public Result register(User user) {
        try{
            log.info("查询用户是否存在");
            User N=userCliMapper.getByNumber(user.getNumber());
            User U=userCliMapper.getByUid(user.getUid());
            if (U!=null||N!=null){
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT).Msg("手机号或身份证已使用");
            }
            log.info("开始添加");
            if ("".equals(user.getPassword())) {
                userCliMapper.add(user);
                log.info("默认密码");
            }else {
                userCliMapper.addforps(user);
            }
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Msg("注册成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_INSERT).Msg("未知异常");
        }
    }

    @Override
    public Result login(User user) {
        try{
            log.info("登录业务");
            User u= userCliMapper.getByLogin(user);
            if (u == null) {
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
            }
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(u);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result wj(String number) {
        try {
            log.info("根据手机号查询账号");
            User byNumber = userCliMapper.getByNumber(number);
            if (byNumber==null){
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT).Msg("该手机号未注册账号");
            }
            String code = NumberCodeUtil.getCode();
            log.info("手机验证码为-------------------->"+code);
//          发送验证码
            redisTemplate.opsForValue().set(number,code,60*2, TimeUnit.SECONDS);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(byNumber);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT).Msg("后台异常");
        }
    }

    @Override
    public Result verify(String number, String code) {
        try{
            String RCode = redisTemplate.opsForValue().get(number);
            if (code.equals(RCode)){
//            验证通过查询
                User byNumber = userCliMapper.getByNumber(number);
                return Result.Builder().Data(byNumber).StatusCode(StatusCode.SUCCESS);
            }else {
                return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT).Msg("后台异常");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT).Msg("后台异常");
        }
    }

    @Override
    public Result setPassword(Integer id, String password) {
        try{
            log.info("设置密码");
            Map<String,Object> map=new HashMap<>();
            map.put("id",id);
            map.put("password",password);
            userCliMapper.setPassword(map);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }

    @Override
    public Result getData(Integer id) {
        try{
            log.info("开始查询七日内记录用户id:-->"+id);
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("start",new DateTime(new Date().getTime()-7*24*60*60*1000));
            map.put("end",new DateTime());
            map.put("id",id);
            List<UserMessage> list=userCliMapper.getData(map);
//            for (UserMessage userMessage : list) {
//                switch(userMessage.getResult()){
//                    case "1": userMessage.setResult("阴性");break;
//                    case "2": userMessage.setResult("阳性");break;
//                    case "0": userMessage.setResult("检测中");break;
//                }
//            }
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result getUser(Integer id) {
        try{
            User u=userCliMapper.getById(id);
            return Result.Builder().StatusCode(StatusCode.SUCCESS).Data(u);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_SELECT);
        }
    }

    @Override
    public Result setUser(User user) {
        try{
            userCliMapper.setUserById(user);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }

    @Override
    public Result isedit(User user) {
        try {
            userCliMapper.setUserById(user);
            return Result.Builder().StatusCode(StatusCode.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.Builder().StatusCode(StatusCode.SQL_EXP_UPDATE);
        }
    }
}
